package com.sctu.EMSApp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.ViewPagerAdapter;
import com.sctu.bean.PlayHotelOneBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 鑫爷您慢点 on 2015/12/17.
 */
public class PlayHotelOneActivity extends Activity implements ViewPager.OnPageChangeListener {
    private TextView title, content, address, phone, price;
    private ViewPager viewPager;
    private ImageView returnView;
    private List<View> viewList = new ArrayList<View>();
    private List<ImageView> imageViewList = new ArrayList<ImageView>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_hotel_one);

        initView();

    }

    /**
     * 跳转当前
     * @param context
     * @param id
     */
    public static void actionStart(Context context,String id){
        Intent intent = new Intent(context, PlayHotelOneActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);
    }

    private void initView() {
        title = (TextView) findViewById(R.id.activity_play_hotel_one_title_text);
        content = (TextView) findViewById(R.id.activity_play_hotel_one_content);
        address = (TextView) findViewById(R.id.activity_play_hotel_one_address);
        phone = (TextView) findViewById(R.id.activity_play_hotel_one_number);
        viewPager = (ViewPager) findViewById(R.id.activity_play_hotel_one_viewpager);
        returnView = (ImageView) findViewById(R.id.activity_play_hotel_one_return_img);

        returnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        GetVolleyHttp.getInstance(this).getStringGETJsonData(UrlConfig.URL_HOTEL_ONE + id,
                listenerListView, errorlistener);
    }

    Response.Listener<String> listenerListView = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            List<PlayHotelOneBean.Results> playHotelList = JSON.parseObject(json, PlayHotelOneBean.class).getResults();
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_play_hotel_one_lay);
            title.setText(playHotelList.get(0).getTitle());
            content.setText(playHotelList.get(0).getContent());
            final String phoneNumber = playHotelList.get(0).getPhone();
            //设置点击文字下划线和颜色
            phone.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
            phone.getPaint().setAntiAlias(true);//抗锯齿
            phone.setTextColor(getResources().getColor(R.color.blue));
            phone.setText(phoneNumber);
            address.setText(playHotelList.get(0).getAddress());


            for (int i = 0; i < playHotelList.get(0).getFileList().size(); i++) {
                LinearLayout linearLayout1 = new LinearLayout(PlayHotelOneActivity.this);
                ViewGroup.LayoutParams ltp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                final ImageView itemImageView = new ImageView(PlayHotelOneActivity.this);
                itemImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                GetVolleyHttp.getInstance(PlayHotelOneActivity.this).setImageLoader(itemImageView,
                        R.drawable.scenerry, R.drawable.error_image, UrlConfig.URL + playHotelList.get(0).getFileList().get(i).getPath());
                linearLayout1.addView(itemImageView, ltp);
                viewList.add(linearLayout1);
                //设置小圆点
                ImageView imageView = new ImageView(PlayHotelOneActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                margin.setMargins(10, 0, 0, 0);
                imageViewList.add(imageView);
                //如果图片只有一张 则不显示
                if (playHotelList.get(0).getFileList().size() > 1) {
                    if (i == 0) {
                        // 默认选中第一张图片
                        imageView.setImageResource(R.drawable.scenic_tour_round_after_click_click);
                    } else {
                        //其他图片都设置未选中状态
                        imageView.setImageResource(R.drawable.scenic_tour_round_after_click_unclick);
                    }
                }
                //将image添加到布局中
                linearLayout.addView(imageView, margin);
            }
            viewPager.setAdapter(new ViewPagerAdapter(viewList));
        }
    };


    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(PlayHotelOneActivity.this, "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();
        }
    };


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int arg0) {
//遍历集合让当前选中图片下的小圆点设置颜色
            for (int i = 0; i < imageViewList.size(); i++) {
                imageViewList.get(i).setImageResource(R.drawable.scenic_tour_round_after_click_click);
                if (arg0 != i) {
                    imageViewList.get(i)
                            .setImageResource(R.drawable.scenic_tour_round_after_click_unclick);
                }
            }

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
